package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.model.Member;
import com.qy.service.MemberService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-MemberController")
@RequestMapping("/manage/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    @PostMapping("/add")
    public Result add(@RequestBody Member member) {
        memberService.save(member);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        memberService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Member member) {
        memberService.update(member);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Member member = memberService.findById(id);
        return ResultGenerator.successResult(member);
    }

    @GetMapping("/list")
    public Result list(PageBean<Member> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Member> list = memberService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //用户管理-列表查询
    @GetMapping("/selectMemberList")
    public Result selectMemberList(PageBean<Map> page, Integer type, String content, Integer state) {

        String nickname = null;
        String name = null;
        String phone = null;
        if (type == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        } else if (type == 1) {
            nickname = content;
        } else if (type == 2) {
            name = content;
        } else if (type == 3) {
            phone = content;
        }
        Member member = new Member();
        member.setNickname(nickname);
        member.setName(name);
        member.setState(state);
        member.setPhone(phone);
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Map> list = memberService.selectMemberList(member);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //用户管理-用户详情
    @GetMapping("/seeMember")
    public Result seeMember(Integer id) {
        List<Map> map = memberService.seeMember(id);
        return ResultGenerator.successResult(map.get(0));
    }

    //    用户管理-禁用/取消禁用
    @PostMapping("/memberState")
    public Result memberState(@RequestBody Member member) {
        if (member.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        memberService.memberState(member);
        return ResultGenerator.successResult();
    }
}
